<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('排会信息') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('会议类型')" prop="meetingType">
        <dict-select v-model="queryParams.meetingType" :placeholder="$t('会议类型')" dictionary="mt_type" />
      </el-form-item>
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input v-model="queryParams.dealName" :placeholder="$t('项目名称')" clearable @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('投资主体')" prop="fundName">
        <el-input v-model="queryParams.fundName" :placeholder="$t('投资主体')" clearable @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['meeting:info:edit']"
          type="success"
          icon="el-icon-s-promotion"
          size="mini"
          :disabled="!meetingList || meetingList.length === 0"
          @click="batchHandle"
        >{{ $t('批量排会') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="mtArrangeInfoList" border tooltip-effect="darkTable" @selection-change="selectChange">
      <el-table-column type="selection" />
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('会议类型')" align="center" prop="meetingType" :formatter="meetingTypeFormat" />
      <el-table-column :label="$t('项目名称')" align="center" prop="dealName" />
      <el-table-column :label="$t('项目负责人')" align="center" prop="personChargeName" />
      <el-table-column :label="$t('投资主体')" align="center" prop="fundName" />
      <el-table-column :label="$t('预计投资金额') + $t('金额单位')" align="center" prop="investAmount">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.investAmount,'w',2,true) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('推送时间')" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime,'{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('排会') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排会信息对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <mtArrangeInfoForm ref="mtArrangeInfoForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listMtArrangeInfo, delMtArrangeInfo, exportMtArrangeInfo } from '@/api/meeting/info/mtArrangeInfo'
import mtArrangeInfoForm from './mtArrangeInfoForm.vue'
import { moneyFormat, parseTime } from '../../../../utils/ruoyi'
export default {
    components: {
        mtArrangeInfoForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 排会信息表格数据
          mtArrangeInfoList: [],
          // 弹出层标题
          title: '排会信息',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              meetingId: undefined,
              dealId: undefined,
              companyId: undefined,
              mtDate: undefined,
              mtTime: undefined,
              mtMinutesPerson: undefined,
              regularAttendees: undefined,
              dealTeamMembers: undefined,
              innerMember: undefined,
              outerMember: undefined,
              outerEmail: undefined,
              mtAddress: undefined,
              sendUser: undefined,
              ccUser: undefined,
              actTitle: undefined,
              applyContent: undefined,
              pageNum: 1,
              pageSize: 10,
            dealName: undefined,
            fundName: undefined,
            delFlag: '2',
            orderByColumn: 'a.create_time desc'
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
        meetingTypeOptions: [],
        // 批量会议列表
        meetingList: []
      }
  },
  created() {
    this.getDicts('mt_type').then(res => {
      this.meetingTypeOptions = res.data
    })
      this.getList()
  },
  methods: {
    parseTime,
    moneyFormat,
    /** 查询排会信息列表 */
    getList() {
        this.loading = true
        listMtArrangeInfo(this.queryParams).then(response => {
            this.mtArrangeInfoList = response.rows
            this.total = response.total
            this.loading = false
          // 刷新sheet菜单页条数
          if (this.$parent.$parent.$parent.gainMtAiCount !== undefined) {
            this.$parent.$parent.$parent.gainMtAiCount()
          }
        }).catch(() => {
            this.loading = false
        })
    },

    meetingTypeFormat(row) {
      return this.selectDictLabel(this.meetingTypeOptions, row.meetingType)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            meetingId: undefined,
            dealId: undefined,
            companyId: undefined,
            mtDate: undefined,
            mtTime: undefined,
            mtMinutesPerson: undefined,
            regularAttendees: undefined,
            dealTeamMembers: undefined,
            innerMember: undefined,
            outerMember: undefined,
            outerEmail: undefined,
            mtAddress: undefined,
            sendUser: undefined,
            ccUser: undefined,
            actTitle: undefined,
            applyContent: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
          dealName: undefined,
          fundName: undefined
        }
        this.resetForm('form')
    },
    // 多选触发的操作
    selectChange(selection) {
      console.log(selection)
      this.meetingList = selection
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.mtArrangeInfoForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            row.closeFlag = '2'
            this.$refs.mtArrangeInfoForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delMtArrangeInfo(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportMtArrangeInfo(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    // 批量排会
    batchHandle() {
      var isSame = this.arraySomeObject(this.meetingList, 'investId')
      if (!isSame) {
        this.$confirm(this.$t('当前有不同的投资主体，是否继续进行排会?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(() => {
          console.log('继续排会')
          this.formOpen = true
          this.$nextTick(() => {
            this.$refs.mtArrangeInfoForm.batchInit(this.meetingList, '2')
          })
        })
      } else {
        this.formOpen = true
        this.$nextTick(() => {
          this.$refs.mtArrangeInfoForm.batchInit(this.meetingList, '2')
        })
      }
    },
    // 判断这组数据中，某个值是否想等
    arraySomeObject(array = [], key) {
      const values = new Set()
      for (const obj of array) {
        values.add(obj[key])
      }
      if (values.size === 1) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>
